<?php
/**
 */
class UsersTable extends Doctrine_Table
{
		public function deActive($userid)
	{
	    //echo $email;exit;
		$query = new Doctrine_Query();
		$query->update('Users')
			  ->set('status',"'0'")
			  ->where("user_id='".$userid."'");
			  //echo $query;exit;
		return $query->execute();
	}
	public function loginCheck($email,$password)
	{
	    //	echo $username." ".$password;exit;
	    $query=new Doctrine_Query();
		$query->select('count(u.user_id) as numCount')
		       ->from('Users u')
			   ->where("u.email='".$email."'")
          	   ->andWhere("u.password='".$password."'");
			   //echo $query;exit;
			   	$result = $query->execute();
				return $result[0];
				
	}
	public function validateUser($username)
	{
	    //	echo $username." ".$password;exit;
	    $query=new Doctrine_Query();
		$query->select('count(u.user_id) as numCount')
		       ->from('Users u')
			   ->where("u.username='".$username."'");
          	    //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				return $result[0];
				
	}
		public function getUserId($email)
	{
	     $query = new Doctrine_Query();
		$query ->select('u.*')
			   ->from('Users u')
			   ->where("u.email='".$email."'")
			   ->limit(1);
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	}
	public function getUserIdByEmail($email)
	{
		$query = new Doctrine_Query();
		$query ->select('u.user_id,u.first_name')
			   ->from('Users u')
			   ->where("u.email='".$email."'")
			   ->limit(1);
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];
	}
	public function getEmail($user_id)
	{
		$query = new Doctrine_Query();
		$query ->select('u.*')
			   ->from('Users u')
			   ->where("u.user_id='".$user_id."'")
			   ->limit(1);
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		if(count($result)>0)
		return $result[0];
		return 0;
	}
	 public function checkUniqueRecord($email)
	{
	    //echo $email;exit;
		$query=new Doctrine_Query();
		$query->select('count(u.user_id) as numCount')
			  ->from('Users u')
			  ->where("u.email='".$email."'");
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0];	  
	}
	public function getMembersByMailId($email)
	{
	    	
	    $query=new Doctrine_Query();
		$query->select('u.*')
		       ->from('Users u')
			   ->where("u.email='".$email."'");
			   //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				return $result[0];
				
	}
	public function getPassword($user_id)
	{
	    	
	    $query=new Doctrine_Query();
		$query->select('u.password')
		       ->from('Users u')
			   ->where("u.user_id='".$user_id."'");
			   //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				return $result[0];
				
	}
	public function updatePassword($password,$user_id)
	{
		$query = new Doctrine_Query();
		$query->update('Users u')
			  ->set('password',"'".$password."'")
			  ->where("u.user_id='".$user_id."'");
		return $query->execute();	  
	}
	public function addUser($data)
	{
		$Users = new Users();
		$Users ->first_name = $data['firstname'];
		$Users ->username = $data['username'];
		$Users ->email = $data['email'];
		$Users ->password = $data['password'];
		$Users ->status = 1;
		$Users ->anonymous = 0;
		
        //echo '<pre>';print_r($Expressions);exit;
		$Users ->save();
		return $Users->user_id;
	}
	public function getName($user_id)
	{
		$query = new Doctrine_Query();
		$query ->select('u.first_name')
			   ->from('Users u')
			   ->where("u.user_id='".$user_id."'")
			   ->limit(1);
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result[0]['first_name'];
	}
	public function getContacts($user_id)
	{
		$query = new Doctrine_Query();
		$query->select('u.first_name as first_name,u.user_id as user_id')
		       ->from('Users u')
			   ->leftJoin('u.UserDetails d ON d.user_id=u.user_id')
			   ->where('u.user_id NOT IN (SELECT c.contact_id FROM Contacts c WHERE user_id = '.$user_id.' GROUP BY c.user_id )')
			   ->andWhere("u.status = '1'")
			   ->groupBy('u.user_id');
				//echo $query->getSqlQuery();exit;
			 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
			return $result;
	        
	}
	////////function to search Users for connections
	public function getUsersBySearch($name,$current_page,$results_per_page)
	{
		$name = addslashes($name);
		$query = new Doctrine_Query();
		$query->select("u.user_id,u.first_name as name,d.photo as photo")
		      ->from('Users u')
			  ->leftJoin('u.UserDetails d')
			  ->where("u.first_name LIKE '%$name%'")
			  ->andWhere("u.status = '1'")
			  ->orderBy('u.first_name');
		//echo $query->getSqlQuery();exit;
		require_once('../database/models/dup_pager.php');
		$pager = new Pager($current_page,$results_per_page);
		$result = $pager->pagination($query);
		return $result;	
	}
	
}